package com

import org.dom4j.Node
import org.dom4j.io.OutputFormat
import org.dom4j.io.XMLWriter
import java.io.OutputStream

class MyXMLWriter(out: OutputStream?, val format: OutputFormat) : XMLWriter(out, format) {

    override fun writeComment(text: String) {
//        super.writeComment(text)

        if (format.isNewlines) {
            println()
            indent()
        }

        writer.write("<!-- ")
        writer.write(text)
        writer.write(" -->")

        lastOutputNodeType = Node.COMMENT_NODE.toInt()
    }

    override fun escapeAttributeEntities(text: String): String {
//        return super.escapeAttributeEntities(text);
        val quote = this.outputFormat.attributeQuoteCharacter
        val buffer = StringBuffer()
        var block: CharArray? = null
        var last = 0
        val size = text.length

        var i = 0
        while (i < size) {
            var entity: String? = null
            val c = text[i]

            when (c) {
                '<' -> entity = "&lt;"

                '>' -> entity = "&gt;"

                '\'' -> if (quote == '\'') {
                    entity = "&apos;"
                }

                '\"' -> if (quote == '\"') {
                    entity = "&quot;"
                }

                '&' -> entity = "&amp;"

//                '\n' -> entity = "&#x000A;"
                '\n',
                '\t', '\r' -> {}
                else -> if ((c.code < 32) || shouldEncodeChar(c.code)) {
                    entity = "&#" + c.code + ";"
                }

            }
            if (entity != null) {
                if (block == null) {
                    block = text.toCharArray()
                }

                buffer.append(block, last, i - last)
                buffer.append(entity)
                last = i + 1
            }
            i++
        }

        if (last == 0) {
            return text
        }

        if (last < size) {
            if (block == null) {
                block = text.toCharArray()
            }

            buffer.append(block, last, i - last)
        }

        val answer = buffer.toString()
        buffer.setLength(0)

        return answer
    }

    override fun escapeElementEntities(text: String): String {
//        return super.escapeElementEntities(text);
        var block: CharArray? = null
        var last = 0
        val size = text.length

        val buffer = StringBuffer()

        var i = 0
        while (i < size) {
            var entity: String? = null
            val c = text[i]

            when (c) {
                '<' -> entity = "&lt;"

                '>' -> entity = "&gt;"

                '&' -> entity = "&amp;"

//                '\n' -> entity = "&#x000A;"
                '\n',
                '\t', '\r' ->                     // don't encode standard whitespace characters
                    if (preserve) {
                        entity = c.toString()
                    }

                else -> if ((c.code < 32) || shouldEncodeChar(c.code)) {
                    entity = "&#" + c.code + ";"
                }

            }
            if (entity != null) {
                if (block == null) {
                    block = text.toCharArray()
                }

                buffer.append(block, last, i - last)
                buffer.append(entity)
                last = i + 1
            }
            i++
        }

        if (last == 0) {
            return text
        }

        if (last < size) {
            if (block == null) {
                block = text.toCharArray()
            }

            buffer.append(block, last, i - last)
        }

        val answer = buffer.toString()
        buffer.setLength(0)

        return answer
    }
}
